<?php

class Fund
{
	static $status = array(
		0 => 'อยู่ระหว่างดำเนินการ',
		1 => 'รอการตรวจสอบ',
		2 => 'เสร็จสมบูรณ์',
		3 => 'แก้ไข',
	);
	
	static function delete_all($fund_id, $agency_id)
	{
		$ci = get_instance();
		$register_ids = $ci->db->getarray('SELECT ID FROM REGISTERS WHERE FUND_ID = ? AND AGENCY_ID = ?', array($fund_id, $agency_id));
		foreach ($register_ids as $register_id) {
			$ci->db->execute('DELETE FROM REGISTERS WHERE ID = ?', array($register_id));
			$ci->db->execute('DELETE FROM REGISTER_DETAILS WHERE REGISTER_ID = ?', array($register_id));	
		}
		
		$balance_ids = $ci->db->getarray('SELECT ID FROM BALANCES WHERE FUND_ID =? AND AGENCY_ID = ?', array($fund_id, $agency_id));
		foreach ($balance_ids as $balance_id) {
			$ci->db->execute('DELETE FROM BALANCES WHERE ID = ?', array($balance_id));
			$ci->db->execute('DELETE FROM BALANCE_DETAILS WHERE BALANCE_ID = ?', array($balance_id));
			$ci->db->execute('DELETE FROM BALANCE_CHECKS WHERE BALANCE_ID = ?', array($balance_id));	
		}
		
		$ci->db->execute('DELETE FROM STATUS WHERE FUND_ID = ? AND AGENCY_ID = ?', array($fund_id, $agency_id));
	}
	
	static function convert_to_year_budget($year, $month)
	{
		if($month > 9) $year += 1;
		return $year;
	}
	
	static function convert_to_real_year($year, $month)
	{
		if($month > 9) $year -= 1;
		return $year;
	}
	
	static function real_year_to_range($year, $month)
	{
		return array($year, $year + 1);
	}
	
	static function fix_budget($y, $m, $is_date_thai = FALSE)
	{
			$y = ($is_date_thai) ? $y - 543 : $y;
			if($m >= 10)
			{
				return array($y.'-10-01', $y.'-'.$m.'-'.date('t',mysql_to_unix($y.'-'.$m.'-1'))); 
			}
			else
			{
				return array(($y-1).'-10-01', $y.'-'.$m.'-30'); 
			}
	}
	
	static function lastest($fund_id, $agency_id, $field = null)
	{
		$ci = get_instance();
		$sql = 'SELECT STATUS_YEAR AS YEAR, STATUS_MONTH AS MONTH FROM STATUS WHERE FUND_ID = ? AND AGENCY_ID = ? ORDER BY YEAR DESC, MONTH DESC';
		$row = $ci->db->getrow($sql, array($fund_id, $agency_id));
		dbConvert($row);
		if($row['month'] > 9) $row['year'] += 1;
		if($row['year'])
		{
			$row['year'] += 543;
			return ($field) ? $row[$field] : $row['year'].'/'.$row['month'];
		}
		else
		{
			if($field == 'year')
			{
				return get_current_year_budget(true);
			}
			elseif($field == 'month')
			{
				return date('m');
			}
			else
			{
				return get_current_year_budget(TRUE).'/'.date('m');
			}
		}
	}
	
	static function first_use($fund_id, $agency_id)
	{
		$ci = get_instance();
		$ids = $ci->db->getone('SELECT COUNT(ID) AS IDS FROM STATUS WHERE FUND_ID = ? AND AGENCY_ID = ?', array($fund_id, $agency_id));
		return ($ids <= 1 and $agency_id > 2) ? TRUE : FALSE;
	}
	
	static function first_budget($fund_id, $agency_id)
	{
		$ci = get_instance();
		$ci->db->debug = false;
		$row = $ci->db->getrow('SELECT * FROM BALANCES WHERE FUND_ID = ? AND AGENCY_ID = ? ORDER BY YEAR_RECORD ASC, MONTH_RECORD ASC', array($fund_id, $agency_id));
		$ci->db->debug = false;
		$year = $row['YEAR_RECORD'] ? $row['YEAR_RECORD'] + 543 : date('Y') + 543;
		$month = $row['MONTH_RECORD'] ? $row['MONTH_RECORD'] : date('m');
		return '<li>'.anchor('banking_balance/form/'.$fund_id.'/'.$year.'/'.$month.'/'.$agency_id, 'ยอดยกมาครั้งแรก').'</li>';
	}
	
	static function status($status, $style = FALSE)
	{
		$number = $status ? $status : 0;
		return $style ? '<span class="status-'.$number.'">'.self::$status[$number].'</span>' : self::$status[$number];
	}

	static function save_status($fund_id, $agency_id)
	{
		$ci = get_instance();
		$ci->load->model('voucher/status_model', 'status');
		$status = $ci->db->execute('SELECT * FROM STATUS WHERE FUND_ID = ? AND AGENCY_ID = ? ORDER BY STATUS_YEAR DESC, STATUS_MONTH DESC', array($fund_id, $agency_id));
		dbConvert($status);
		if($status['id'])
		{
			$data = array(
				'fund_id' => $status['fund_id'],
				'agency_id' => $status['agency_id'],
				'user_id' => login_data(),
				'status_year' => $status['status_year'],
				'status_month' => $status['month'],
				'status' => 1
			);
			$ci->status->save($data);	
		}
	}
	
	static function fund_name($fund_id)
	{
		$ci = get_instance();
		$ci->load->model('voucher/fund_model');
		return $ci->fund_model->get_one('NAME', $fund_id);
	}
	
	static function agency_name($agency_id)
	{
		$ci = get_instance();
		$ci->load->model('user/agency_model');
		return $ci->agency_model->get_one('NAME', $agency_id);
	}
	
	static function notice($fund_id)
	{
		$CI = get_instance();
		$user = login_data();
		$status = $CI->db->getRow('select * from status where id = (select max(id) from status where fund_id = '.$fund_id.' and agency_id = '.$user['agency_id'].')');
		dbConvert($status);
		
		$date = $status['status_year'].'-'.substr('0'.($status['status_month']),-2).'-'.substr('0'.get_one('value','settings','fund_id',$fund_id),-2);
		$status_date = date("Y-m-d",strtotime(date("Y-m-d", strtotime($date)) . " +1 month"));
		$current_date = date('Y-m-d');
		$remain = DateDiff($current_date, $status_date);
		
		if($status['id'])
		{
		$html = '<div class="header-notice">';
		$html .= ($remain >=0)?'<img src="themes/fund/images/icon/time.png" style="width:40px;height:40px;" />':'<img src="themes/fund/images/warning.png" style="width:40px;height:40px;" />';
		$html .= get_one('name','funds','id',$fund_id).'<br />';
		$html .= 'กำหนดส่งข้อมูลภายในวันที่ '.db_to_th($status_date).'<br />';
		$html .= self::$status[$status['status']].'<br />';
		if($remain >= -3)
		{
			if($remain < 0)
			{
				$html .= '<span class="red">เลยกำหนดเวลาในการส่งข้อมูล '.($remain * -1).' วัน</span><br />';
			}
			else
			{
				$html .= 'เหลือเวลาในการส่งข้อมูล '.$remain.' วัน<br />';
			}
			//if(!Fund::is_lock($fund_id, $user['agency_id'], $status['status_year'], $status['status_month'])) $html .= '<a style="display:block; float:right; margin:5px 0 0 0;" class="button" href="dashboard/complete/'.$fund_id.'/'.$status['status_year'].'/'.$status['status_month'].'"><span class="icon_text send"></span>ส่งข้อมูล</a>';
		}
		else
		{
			$html .= '<span class="red">เลยกำหนดเวลาในการส่งข้อมูล '.($remain * -1).' วัน</span><br />';
			
			
			
			/*
				$CI->load->model('setting/agency_detail_model', 'agency_detail');
				$data = array(
					'FUND_ID' => $fund_id,
					'AGENCY_ID' => $user['agency_id'],
					'IS_LOCK' => 1
				);
				$CI->db->execute('DELETE FROM AGENCY_DETAILS WHERE FUND_ID = ? AND AGENCY_ID = ?', array($fund_id, $user['agency_id']));
				
				$CI->agency_detail->save($data);	
			*/
			
			/*
			if(($CI->db->getOne('select is_lock from agency_details where fund_id = ? and agency_id = ?',array($fund_id,$user['agency_id']))=='0')OR($status['status']=='3'))
			{
				if($status['status']==3)
				{
					$html .= '<a style="display:block; float:right; margin:5px 0 0 0;" class="button" href="dashboard/complete/'.$fund_id.'/'.$status['status_year'].'/'.$status['status_month'].'"><span class="icon_text send"></span>ส่งข้อมูล</a>';
				}
				else
				{
					$html .= '<a style="display:block; float:right; margin:5px 0 0 0;" class="button" href="dashboard/complete/'.$fund_id.'/'.$status['status_year'].'/'.$status['status_month'].'"><span class="icon_text send"></span>ส่งข้อมูล</a>';
				}
			}*/
			
		}
		if(!Fund::is_lock($fund_id, $user['agency_id'], $status['status_year'], $status['status_month'])) $html .= '<a style="display:block; float:right; margin:5px 0 0 0;" class="button" href="dashboard/complete/'.$fund_id.'/'.$status['status_year'].'/'.$status['status_month'].'"><span class="icon_text send"></span>ส่งข้อมูล</a>';
		$html .= '<br style="clear:both;" />';
		$html .= '</div>';
		return $html;
		}
	}
	
	static function is_expire($fund_id, $agency_id)
	{
		$CI = get_instance();
		$status = $CI->db->getRow('select * from status where id = (select max(id) from status where fund_id = '.$fund_id.' and agency_id = '.$agency_id.')');
		dbConvert($status);
		
		$date = $status['status_year'].'-'.substr('0'.($status['status_month']),-2).'-'.substr('0'.get_one('value','settings','fund_id',$fund_id),-2);
		$status_date = date("Y-m-d",strtotime(date("Y-m-d", strtotime($date)) . " +1 month"));
		$current_date = date('Y-m-d');
		$remain = DateDiff($current_date, $status_date);
		return $remain;
	}
	
	static function is_lock($fund_id, $agency_id, $year, $month)
	{
		/*
		$ci = get_instance();
		$status = $ci->db->getone('SELECT STATUS FROM STATUS WHERE ID = (SELECT max(ID) FROM STATUS WHERE FUND_ID = ? AND AGENCY_ID = ? AND STATUS_YEAR = ? AND STATUS_MONTH = ?)', array($fund_id, $agency_id, $year, $month));
		return (!in_array($status, array(1, 2)) /*and self::is_agency_lock($fund_id, $agency_id) == FALSE ) ? FALSE : TRUE;
		*/
		$ci = get_instance();
		$status = $ci->db->getone('SELECT STATUS FROM STATUS WHERE ID = (SELECT max(ID) FROM STATUS WHERE FUND_ID = ? AND AGENCY_ID = ? AND STATUS_YEAR = ? AND STATUS_MONTH = ?)', array($fund_id, $agency_id, $year, $month));
		return in_array($status, array(1, 2)) ? TRUE : FALSE;
	}
	
	static function is_agency_lock($fund_id, $agency_id)
	{
		$ci = get_instance();
		$is_lock = $ci->db->getone('SELECT IS_LOCK FROM AGENCY_DETAILS WHERE FUND_ID = ? AND AGENCY_ID = ?', array($fund_id, $agency_id));
		$is_lock = is_null($is_lock) ? 1 : $is_lock; 
		return ($is_lock == 0 or self::is_expire($fund_id, $agency_id) >= 0) ? FALSE : TRUE;
	}
}

?>